package ore.example.pokemoninfo.GUI;

import ore.example.pokemoninfo.item.PokemonEvolveItem;
import ore.example.pokemoninfo.utils.TradeEvolutionFinder;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import xyz.xenondevs.invui.gui.PagedGui;
import xyz.xenondevs.invui.item.Item;
import xyz.xenondevs.invui.item.ItemProvider;
import xyz.xenondevs.invui.item.builder.ItemBuilder;
import xyz.xenondevs.invui.item.impl.SimpleItem;
import xyz.xenondevs.invui.item.impl.controlitem.PageItem;
import xyz.xenondevs.invui.window.Window;

import java.util.ArrayList;
import java.util.List;

public class PokemonTradeGui {
    // 上一页按钮
    public static class BackItem extends PageItem {
        public BackItem() {
            super(false);
        }

        @Override
        public ItemProvider getItemProvider(PagedGui<?> gui) {
            return new ItemBuilder(Material.RED_STAINED_GLASS_PANE)
                    .setDisplayName("§c上一页")
                    .addLoreLines(gui.hasPreviousPage()
                            ? "§7当前页: §e" + (gui.getCurrentPage() + 1)
                            : "§8已经是第一页");
        }
    }

    // 下一页按钮
    public static class ForwardItem extends PageItem {
        public ForwardItem() {
            super(true);
        }

        @Override
        public ItemProvider getItemProvider(PagedGui<?> gui) {
            return new ItemBuilder(Material.GREEN_STAINED_GLASS_PANE)
                    .setDisplayName("§a下一页")
                    .addLoreLines(gui.hasNextPage()
                            ? "§7跳转至 §e" + (gui.getCurrentPage() + 2)
                            : "§8已经是最后一页");
        }
    }

    public static void open(Player player) {
        List<TradeEvolutionFinder.PokemonPhoto> photos = TradeEvolutionFinder.getAllTradeEvolutionPhotos();
        List<Item> items = new ArrayList<>();

        for (TradeEvolutionFinder.PokemonPhoto photo : photos) {
            items.add(new PokemonEvolveItem(photo.species(), new ItemBuilder(photo.photo())));
        }

        PagedGui<Item> gui = PagedGui.items()
                .setStructure(
                        "# # # # # # # # #",
                        "# x x x x x x x #",
                        "# x x x x x x x #",
                        "# x x x x x x x #",
                        "# x x x x x x x #",
                        "# # # < c > # # #"
                )
                .addIngredient('#', new SimpleItem(new ItemBuilder(Material.GRAY_STAINED_GLASS_PANE).setDisplayName(" ")))
                .addIngredient('x', xyz.xenondevs.invui.gui.structure.Markers.CONTENT_LIST_SLOT_HORIZONTAL)
                .setContent(items)
                .addIngredient('<', new BackItem())
                .addIngredient('>', new ForwardItem())
                .build();

        Window window = Window.single()
                .setViewer(player)
                .setTitle("§b通信进化宝可梦列表")
                .setGui(gui)
                .build();

        window.open();
    }
}
